<template>
  <div class="userList-container">
    <el-form
      :model="formData"
      inline
      class="myForm"
    >
      <el-form-item prop="userId">
        <el-input
          v-model="formData.userId"
          placeholder="请输入用户ID"
        ></el-input>
      </el-form-item>
      <el-form-item prop="valueId">
        <el-input
          v-model="formData.valueId"
          placeholder="请输入商品ID"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search" type="primary" icon="el-icon-search">
          查找
          </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="tableLoading" :data="tableData" border class="myTab">
      <el-table-column
        align="center"
        label="收藏ID"
        prop="id"
        width="100px"
      ></el-table-column>
      <el-table-column
        align="center"
        label="用户ID"
        prop="userId"
      ></el-table-column>
      <el-table-column
        align="center"
        label="商品ID"
        prop="valueId"
      ></el-table-column>
      <el-table-column
        align="center"
        label="添加时间"
        prop="addTime"
      ></el-table-column>
    </el-table>

    <el-pagination
    class="myStyle"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 50, 100]"
      :page-size="pageInfo.limit"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
    >
    </el-pagination>
  </div>
</template>

<script>
// 请求列表的url http://192.168.200.251:8888/admin/collect/list

import axios from "axios";

export default {
  name: "userCollect",
  componentTitle: "用户收藏",
  data() {
    return {
      tableLoading: false,
      pageInfo: {
        page: 1,
        limit: 10,
        total: 0,
      },
      formData: {
        userId: "",
        valueId: "",
      },
      tableData: [],
    };
  },
  mounted() {
    this.search();
  },
  filters: {
    tableFilter(value, options) {
      let index = options.findIndex((item) => {
        return Number(item.value) === Number(value);
      });

      if (index < 0) {
        return "---";
      } else {
        return options[index].label;
      }
    },
  },
  methods: {
    search() {
      this.tableLoading = true;

      axios({
        url: "http://192.168.200.251:8888/admin/collect/list",
        method: "get",
        params: {
          page: this.pageInfo.page,
          limit: this.pageInfo.limit,
          ...this.formData
        },
        headers: {
          "Shopping-Admin-Token": sessionStorage.getItem(
            "Shopping-Admin-Token"
          ),
        },
      }).then((res) => {
        this.tableLoading = false;
        this.tableData = res.data.data.list;

        this.pageInfo.total = res.data.data.total;
        console.log(res.data.data.list);
      });
    },
    // 每页多少条切换
    handleSizeChange(value) {
      this.pageInfo.limit = value;

      this.search();
    },
    // 当前页码的切换
    handleCurrentChange(value) {
      this.pageInfo.page = value;

      this.search();
    },
  },
};
</script>

<style lang="scss" scoped>
.myStyle{
  margin-top: 70px;
  margin-left: 80px;
}

.myForm{
display: flex;
justify-content: flex-end; 
margin-top: 10px;
margin-right: 22px;
}

.myTab{
  width: 95%;
  text-align: center;
  margin: 0 auto;

}


</style>

